package com.jyxd.web.mq;

import cn.hutool.core.lang.Snowflake;
import com.jyxd.web.data.dto.NursingTaskDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class NursingTaskExecuteSender {

    private final Logger logger = LoggerFactory.getLogger(NursingTaskExecuteSender.class);
    private final AmqpTemplate rabbitTemplate;

    @Value("${icu.job.mq.nurse.normal.routingKey}")
    private String routingKey;
    @Value("${icu.job.mq.nurse.normal.exchange}")
    private String exchange;
    private final Snowflake snowflake;

    public NursingTaskExecuteSender(RabbitTemplate rabbitTemplate, Snowflake snowflake) {
        this.rabbitTemplate = rabbitTemplate;

//        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
//            logger.info("消息唯一标识：" + correlationData.getId());
//            logger.info("确认结果：" + ack);
//            logger.info("失败原因：" + cause);
//        });
//
//        rabbitTemplate.setReturnsCallback(returnedMessage -> logger.info("returnedMessage:{}", returnedMessage));
        this.snowflake = snowflake;
    }

    public void send(NursingTaskDTO nursingTaskDTO) {
        logger.info("NursingTaskDTO({}):{}", nursingTaskDTO.hashCode(), nursingTaskDTO);
        rabbitTemplate.convertAndSend(exchange, routingKey, nursingTaskDTO, message -> {
            message.getMessageProperties().setMessageId(snowflake.nextIdStr());
            return message;
        });
    }
}